package com.wing.api.query.cellInventorySpace.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.api.ApplicationContextUtil;
import com.wing.api.query.cellInventorySpace.BaseQueryCellInventorySpaceService;
import com.wing.cabinet.model.entity.CellInventoryProduct;
import com.wing.cabinet.model.response.CellInventorySpaceVO;
import com.wing.cabinet.model.response.EnterpricceIdVO;
import com.wing.cabinet.service.CellInventorySpaceService;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.feign.EnterpriseClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 标准领新柜库存列表(按货道)查询接口
 */
@Service
@Slf4j
public class StandardQueryCellInventorySpaceServiceImpl implements BaseQueryCellInventorySpaceService {

    @Override
    public PageResult<CellInventorySpaceVO> queryList(JSONObject jsonObject, EnterpricceIdVO enterpricceIdVo, CellInventorySpaceService cellInventorySpaceService) {
        log.info("请求参数,{}", jsonObject.toJSONString());
        //此处需要做接口签名验证
        //sign = md5（参数1+参数2+参数3+key）
        String sourceSign = jsonObject.getString("sign"); // 签名值
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String cabCode = jsonObject.getString("cabCode");
        String belongCabinet = jsonObject.getString("belongCabinet");
        String spaceCode = jsonObject.getString("spaceCode");
        String skuCode = jsonObject.getString("skuCode");
        String oldSpecCode = jsonObject.getString("oldSpecCode");

        if(StringUtils.isBlank(sourceSign)){
            throw new BusinessException("签名值为空！");
        }

        if(page == null || page == 0){
            throw new BusinessException("page参数传值异常！");
        }
        if(limit == null || limit == 0){
            throw new BusinessException("limit参数传值异常！");
        }

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(page);
        tempStr.append(limit);
        if(StringUtils.isNotBlank(cabCode)){
            tempStr.append(cabCode);
        }
        if(StringUtils.isNotBlank(belongCabinet)){
            tempStr.append(belongCabinet);
        }
        if(StringUtils.isNotBlank(spaceCode)){
            tempStr.append(spaceCode);
        }
        if(StringUtils.isNotBlank(skuCode)){
            tempStr.append(skuCode);
        }
        if(StringUtils.isNotBlank(oldSpecCode)){
            tempStr.append(oldSpecCode);
        }

        tempStr.append(enterpricceIdVo.getKey());
        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
        log.info("签名目的串,{}", desSign);
        if(!desSign.equals(sourceSign)){
            throw new BusinessException("签名不通过！");
        }

        Page<CellInventoryProduct> pg = new Page<>(page, limit);
        Map queryParam = new HashMap();
        queryParam.put("sort", "create_time");
        queryParam.put("order", "asc");
        queryParam.put("cabCode", cabCode);
        queryParam.put("belongCabinet", belongCabinet);
        queryParam.put("spaceCode", spaceCode);
        queryParam.put("skuCode", skuCode);
        queryParam.put("oldSpecCode", oldSpecCode);
        IPage<CellInventorySpaceVO> list = cellInventorySpaceService.pageList(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @Override
    public JsonResult queryDetail(Long id, EnterpricceIdVO enterpricceIdVo, String sign) {
        log.info("请求参数,id="+id+",sign="+sign);
        //此处需要做接口签名验证
        //sign = md5（参数1+参数2+参数3+key）

        if(StringUtils.isBlank(sign)){
            throw new BusinessException("签名值为空！");
        }

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(id);
        tempStr.append(enterpricceIdVo.getKey());
        log.info("签名源串,{}", tempStr.toString());
        String desSign = DigestUtil.md5Hex(tempStr.toString());
        log.info("签名目的串,{}", desSign);
        if(!desSign.equals(sign)){
            throw new BusinessException("签名不通过！");
        }
        EnterpriseClient enterpriseClient = (EnterpriseClient) ApplicationContextUtil.getBean("enterpriseClientFallback");

        return enterpriseClient.foreignProductInfoDetail(id);
    }

    public static void main(String[] args) {
//        String key = "e10adc3949ba59abbe56e057f20f883e";
        String key = "$2a$10$fxpgo/SLFHCeNThnfF/q7uvoZJOxbJBNNI4.dshzlntgBIHKI9eb."; // 测试
//        String key = "$2a$10$A4q7sKe4/3GekaIIHLTqG.Gd4y8PhX4yLNleuHK9fvvyWq1vodM6e";
        Integer page = 1;
        Integer limit = 10;

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(page);
        tempStr.append(limit);

        tempStr.append(key);
//        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
//        log.info("签名目的串,{}", desSign);
        System.out.println(desSign);
    }

}
